from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# open Chrome browser and navigate to login page
driver = webdriver.Chrome()
driver.get("https://the-internet.herokuapp.com/login")

# enter valid username and password and click login button
username = driver.find_element(By.ID,"username")
password = driver.find_element(By.ID,"password")
username.send_keys("tomsmith")
password.send_keys("SuperSecretPassword!")

login_button = driver.find_element(By.CSS_SELECTOR,".radius")
login_button.click()
time.sleep(5)

# verify redirection to home page and display of username
assert "The Internet" in driver.title
assert "You logged into a secure area!" in driver.page_source

# logout and verify redirection to login page
logout_button = driver.find_element(By.CLASS_NAME,"button.secondary.radius")
logout_button.click()
time.sleep(1)
assert "The Internet" in driver.title


# enter invalid username and password and check response
username = driver.find_element(By.ID,"username")
password = driver.find_element(By.ID,"password")
username.send_keys("Revive9331")
time.sleep(5)
password.send_keys("14536756850")
time.sleep(5)
login_button = driver.find_element(By.CSS_SELECTOR,".radius")
login_button.click()

assert "Your username is invalid!" in driver.page_source
